<?php
namespace api\toilet\controller;

use api\admin\model\ToiletModel;
use api\toilet\logic\PushModel;
use api\toilet\logic\DistanceModel;
use think\Db;
use cmf\controller\RestBaseController;

//H5版本地图定位
class MasterLocalController extends RestBaseController
{
    //MySQL 增加经纬度
    
    /**
     * [ToiletLocalList 获取当前公厕列表包含经纬度]
     * @Author   HUI
     * @DateTime 2020-02-04
     * @version  [version]
     */
    public function ToiletLocalList()
    {

        $toiletRes = null;
        //接收并处理数据
        $param = $this->request->param();
        $param['type']  = isset($param['type']) ? $param['type'] : 0;
        $longitude  = isset($param['longitude']) ? $param['longitude'] : '114.180736';
        $latitude  = isset($param['latitude']) ? $param['latitude'] : '33.734699';
        //获取当前code绑定的用户
        if (!empty($param['code']) && $longitude && $latitude) {
            $user = DB::name('user')->where('user_code', $param['code'])->find();
            //获取当前用户绑定的公厕
            if (!empty($user)) {
                $Toilet = new ToiletModel();
                $toiletRes = $Toilet
                ->field('title,mac,id,longitude,latitude')
                ->where(['user_id' => $user['id'], 'status' => 1])
                ->select()->toArray();
                //处理当前公厕数据
                if (!empty($toiletRes)) {
                    
                    $distance = new DistanceModel();
                    $toiletRes = $distance->getToiletDistance(
                        $toiletRes,
                        $longitude,
                        $latitude,
                        $param['type']
                    );
                }
            }
        }
        return $this->success('列表获取成功', $toiletRes);

    }

}
